#include "lcd_init.h"
#include "stdio.h"
#include <string.h>
// #include "app.h"

typedef void (*FunType)(void);
//**********************************write function begin********************************************
void wr_cmd_4spi_8bcs(uint8_t par)
{
    LCD_DCXLow();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    // LCD_DCXHigh();
    // HAL_Delay(5);
}
void wr_dat_4spi_8bcs(uint8_t par)
{
    LCD_DCXHigh();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    LCD_DCXHigh();
    // HAL_Delay(1);
}
void wr_cmd_parall1_8(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_dat_parall1_8(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_cmd_parall2_8(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par << 1;

    LCD_WRHigh();
}
void wr_dat_parall2_8(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par << 1;

    LCD_WRHigh();
}
//**********************************read function begin********************************************
void pb_out(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void pb_in(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
uint8_t i801_read_byte(void)
{
    uint8_t v;

    LCD_DCXHigh();
    LCD_RDLow();
    v = GPIOB->IDR;
    LCD_RDHigh();

    return v;
}
void rd_parall1_8(uint8_t c, uint8_t *d, uint32_t l)
{
    uint8_t buf[128];

    wr_cmd_parall1_8(c);
    pb_in();
    for (int i = 0; i < l; i++)
    {
        buf[i] = i801_read_byte();
    }
    LCD_CSHigh();
    pb_out();
    memcpy(d, buf, l);
}
//**********************************write display begin********************************************
void wr_displ_parall1_8(uint8_t color)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = color;
    LCD_WRHigh();
}
void wr_displ_parall2_18(uint16_t color1, uint16_t color2)
{
    LCD_DCXHigh();
    LCD_WRLow();
    if (color1 & 0x02)
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_RESET);
    if (color1 & 0x01)
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_RESET);
    GPIOB->ODR = color2;
    LCD_WRHigh();
}

void Write(uint8_t dc, uint8_t par)
{
    if (dc == Command)
    {
        wr_cmd_4spi_8bcs(par);
    }
    else if (dc == Parameter)
    {
        wr_dat_4spi_8bcs(par);
    }
}

void HW_Reset(void)
{
    LCD_RSTHigh();
    HAL_Delay(120);
    LCD_RSTLow();
    HAL_Delay(120);
    LCD_RSTHigh();
    HAL_Delay(200);
}

void Delay(uint32_t ms)
{
    HAL_Delay(ms);
}

void wr_cmd_3spi_9bcs(uint16_t par)
{
    LCD_CSLow();

    LCD_SCLLow();
    LCD_SDALow();
    LCD_SCLHigh();

    for (uint16_t i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if ((par & 0x80) != 0)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        LCD_SCLHigh();
        par = par << 1;
    }
    LCD_CSHigh();
}

void wr_dat_3spi_9bcs(uint16_t par)
{
    LCD_CSLow();

    LCD_SCLLow();
    LCD_SDAHigh();
    LCD_SCLHigh();

    for (uint16_t i = 0; i < 8; i++)
    {
        LCD_SCLLow();
        if ((par & 0x80) != 0)
        {
            LCD_SDAHigh();
        }
        else
        {
            LCD_SDALow();
        }
        LCD_SCLHigh();
        par = par << 1;
    }
    LCD_CSHigh();
}

void DM_CMDWrite(uint16_t par)
{
    wr_cmd_3spi_9bcs(par);
}

void DM_DATAWrite(uint16_t par)
{
    wr_cmd_3spi_9bcs(par);
}

void DelayMS(uint32_t ms)
{
    HAL_Delay(ms);
}

void DM_Initial(void)
{
    HW_Reset();
    // ST7789P3+BOE2.41 IPS-2.2gamma-20230816
    DM_CMDWrite(0x11);

    DelayMS(120); // ms

    DM_CMDWrite(0x36);
    DM_DATAWrite(0x40);

    DM_CMDWrite(0x3A);
    DM_DATAWrite(0x66); // 0x66->RGB666,0x55->RGB565

    DM_CMDWrite(0xB2);
    DM_DATAWrite(0x0C);
    DM_DATAWrite(0x0C);
    DM_DATAWrite(0x00);
    DM_DATAWrite(0x33);
    DM_DATAWrite(0x33);

    DM_CMDWrite(0xB7);
    DM_DATAWrite(0x35); // VGH=13.26V,VGL=-10.43V

    DM_CMDWrite(0xBB);
    DM_DATAWrite(0x2D);

    DM_CMDWrite(0xC0);
    DM_DATAWrite(0x2C);

    DM_CMDWrite(0xC2);
    DM_DATAWrite(0x01);

    DM_CMDWrite(0xC3);
    DM_DATAWrite(0x0F);

    DM_CMDWrite(0xC4);
    DM_DATAWrite(0x20);

    DM_CMDWrite(0xC6);
    DM_DATAWrite(0x0F);

    DM_CMDWrite(0xD0);
    DM_DATAWrite(0xA7);
    DM_DATAWrite(0xA1);

    DM_CMDWrite(0xD0);
    DM_DATAWrite(0xA4);
    DM_DATAWrite(0xA1);

    DM_CMDWrite(0xD6);
    DM_DATAWrite(0xA1);

    DM_DATAWrite(0xE0);
    DM_DATAWrite(0x0F);
    DM_DATAWrite(0x1B);
    DM_DATAWrite(0x22);
    DM_DATAWrite(0x11);
    DM_DATAWrite(0x11);
    DM_DATAWrite(0x3C);
    DM_DATAWrite(0x42);
    DM_DATAWrite(0x66);
    DM_DATAWrite(0x52);
    DM_DATAWrite(0x20);
    DM_DATAWrite(0x1F);
    DM_DATAWrite(0x1E);
    DM_DATAWrite(0x3E);
    DM_DATAWrite(0x3F);

    DM_DATAWrite(0xE1);
    DM_DATAWrite(0x0F);
    DM_DATAWrite(0x14);
    DM_DATAWrite(0x1B);
    DM_DATAWrite(0x0A);
    DM_DATAWrite(0x0A);
    DM_DATAWrite(0x06);
    DM_DATAWrite(0x41);
    DM_DATAWrite(0x55);
    DM_DATAWrite(0x51);
    DM_DATAWrite(0x21);
    DM_DATAWrite(0x1F);
    DM_DATAWrite(0x1B);
    DM_DATAWrite(0x3B);
    DM_DATAWrite(0x3A);

    DM_CMDWrite(0x21);

    DM_CMDWrite(0x29);

    DM_CMDWrite(0x2C);
}

void gamma(void)
{
    HW_Reset();
    DelayMS(800); // ms

    DM_CMDWrite(0x11);

    DelayMS(800); // ms   200US

    DM_CMDWrite(0x36);
    DM_DATAWrite(0x00);

    DM_CMDWrite(0x3A); // RGB666
    DM_DATAWrite(0x05);

    DM_CMDWrite(0xB0);
    DM_DATAWrite(0x12); // 11
    DM_DATAWrite(0xF0);

    DM_CMDWrite(0xB1);
    DM_DATAWrite(0x40);
    DM_DATAWrite(0x04);
    DM_DATAWrite(0x14);

    DM_CMDWrite(0xB2);
    DM_DATAWrite(0x0C);
    DM_DATAWrite(0x0C);
    DM_DATAWrite(0x00);
    DM_DATAWrite(0x33);
    DM_DATAWrite(0x33);

    DM_CMDWrite(0xB7);
    DM_DATAWrite(0x04);

    DM_CMDWrite(0xBB);
    DM_DATAWrite(0x1F);

    DM_CMDWrite(0xC0);
    DM_DATAWrite(0x2C);

    DM_CMDWrite(0xC2);
    DM_DATAWrite(0x01);

    DM_CMDWrite(0xC3);
    DM_DATAWrite(0x0F); // 1D

    DM_CMDWrite(0xC4);
    DM_DATAWrite(0x20);

    DM_CMDWrite(0xC6);
    DM_DATAWrite(0x0F);

    DM_CMDWrite(0xD0);
    DM_DATAWrite(0xA4);
    DM_DATAWrite(0xA1);

    DM_CMDWrite(0xD6);
    DM_DATAWrite(0xA1);

    DM_CMDWrite(0xE0);
    DM_DATAWrite(0xF0);
    DM_DATAWrite(0x14);
    DM_DATAWrite(0x1F);
    DM_DATAWrite(0x15);
    DM_DATAWrite(0x12);
    DM_DATAWrite(0x11);
    DM_DATAWrite(0x42);
    DM_DATAWrite(0x55);
    DM_DATAWrite(0x54);
    DM_DATAWrite(0x08);
    DM_DATAWrite(0x1B);
    DM_DATAWrite(0x1B);
    DM_DATAWrite(0x24);
    DM_DATAWrite(0x3F);

    DM_CMDWrite(0xE1);
    DM_DATAWrite(0xF0);
    DM_DATAWrite(0x07);
    DM_DATAWrite(0x1D);
    DM_DATAWrite(0x07);
    DM_DATAWrite(0x08);
    DM_DATAWrite(0x3F);
    DM_DATAWrite(0x41);
    DM_DATAWrite(0x54);
    DM_DATAWrite(0x53);
    DM_DATAWrite(0x1E);
    DM_DATAWrite(0x1D);
    DM_DATAWrite(0x18);
    DM_DATAWrite(0x21);
    DM_DATAWrite(0x3F);

    DM_CMDWrite(0x21);

    DM_CMDWrite(0x29);
}

void SPI_TEST(void)
{
    HW_Reset();
    Delay(800); // ms

    DM_CMDWrite(0x11);

    Delay(800); // ms   200US

    DM_CMDWrite(0x36);
    DM_DATAWrite(0x08); // 00

    DM_CMDWrite(0x3A);  // RGB666
    DM_DATAWrite(0x66); // 05

    DM_CMDWrite(0xB0);
    DM_DATAWrite(0x00); // 12
    DM_DATAWrite(0xF0);

    DM_CMDWrite(0xB1);
    DM_DATAWrite(0x40);
    DM_DATAWrite(0x04);
    DM_DATAWrite(0x14);

    DM_CMDWrite(0xB2);
    DM_DATAWrite(0x0C);
    DM_DATAWrite(0x0C);
    DM_DATAWrite(0x00);
    DM_DATAWrite(0x33);
    DM_DATAWrite(0x33);

    DM_CMDWrite(0xB7);
    DM_DATAWrite(0x04);

    DM_CMDWrite(0xBB);
    DM_DATAWrite(0x1F);

    DM_CMDWrite(0xC0);
    DM_DATAWrite(0x2C);

    DM_CMDWrite(0xC2);
    DM_DATAWrite(0x01);

    DM_CMDWrite(0xC3);
    DM_DATAWrite(0x0F); // 1D

    DM_CMDWrite(0xC4);
    DM_DATAWrite(0x20);

    DM_CMDWrite(0xC6);
    DM_DATAWrite(0x0F);

    DM_CMDWrite(0xD0);
    DM_DATAWrite(0xA4);
    DM_DATAWrite(0xA1);

    DM_CMDWrite(0xD6);
    DM_DATAWrite(0xA1);

    DM_CMDWrite(0xE0);
    DM_DATAWrite(0xF0);
    DM_DATAWrite(0x14);
    DM_DATAWrite(0x1F);
    DM_DATAWrite(0x15);
    DM_DATAWrite(0x12);
    DM_DATAWrite(0x11);
    DM_DATAWrite(0x42);
    DM_DATAWrite(0x55);
    DM_DATAWrite(0x54);
    DM_DATAWrite(0x08);
    DM_DATAWrite(0x1B);
    DM_DATAWrite(0x1B);
    DM_DATAWrite(0x24);
    DM_DATAWrite(0x3F);

    DM_CMDWrite(0xE1);
    DM_DATAWrite(0xF0);
    DM_DATAWrite(0x07);
    DM_DATAWrite(0x1D);
    DM_DATAWrite(0x07);
    DM_DATAWrite(0x08);
    DM_DATAWrite(0x3F);
    DM_DATAWrite(0x41);
    DM_DATAWrite(0x54);
    DM_DATAWrite(0x53);
    DM_DATAWrite(0x1E);
    DM_DATAWrite(0x1D);
    DM_DATAWrite(0x18);
    DM_DATAWrite(0x21);
    DM_DATAWrite(0x3F);

    DM_CMDWrite(0x21);

    DM_CMDWrite(0x29);
}

void initial(void)
{
    HW_Reset();

    Delay(120);

    Write(Command, 0x11);

    Delay(120);

    Write(Command, 0x36);
    Write(Parameter, 0x00);

    Write(Command, 0x3A);
    Write(Parameter, 0x06);

    Write(Command, 0xB2);
    Write(Parameter, 0x1F);
    Write(Parameter, 0x1F);
    Write(Parameter, 0x00);
    Write(Parameter, 0x33);
    Write(Parameter, 0x33);

    Write(Command, 0xB7);
    Write(Parameter, 0x00);

    Write(Command, 0xBB);
    Write(Parameter, 0x3F);

    Write(Command, 0xC0);
    Write(Parameter, 0x2C);

    Write(Command, 0xC2);
    Write(Parameter, 0x01);

    Write(Command, 0xC3);
    Write(Parameter, 0x0D);

    Write(Command, 0xC4);
    Write(Parameter, 0x20); // VDV, 0x20:0v

    Write(Command, 0xC6);
    Write(Parameter, 0x13); // 0x13:60Hz

    Write(Command, 0xD0);
    Write(Parameter, 0xA4);
    Write(Parameter, 0xA1);

    Write(Command, 0xD6);
    Write(Parameter, 0xA1); // sleep in后，gate输出为GND

    Write(Command, 0xE0);
    Write(Parameter, 0xF0);
    Write(Parameter, 0x03);
    Write(Parameter, 0x09);
    Write(Parameter, 0x03);
    Write(Parameter, 0x03);
    Write(Parameter, 0x10);
    Write(Parameter, 0x2D);
    Write(Parameter, 0x43);
    Write(Parameter, 0x3F);
    Write(Parameter, 0x33);
    Write(Parameter, 0x0D);
    Write(Parameter, 0x0E);
    Write(Parameter, 0x29);
    Write(Parameter, 0x32);

    Write(Command, 0xE1);
    Write(Parameter, 0xF0);
    Write(Parameter, 0x0C);
    Write(Parameter, 0x10);
    Write(Parameter, 0x0E);
    Write(Parameter, 0x0E);
    Write(Parameter, 0x0A);
    Write(Parameter, 0x2D);
    Write(Parameter, 0x33);
    Write(Parameter, 0x45);
    Write(Parameter, 0x3A);
    Write(Parameter, 0x14);
    Write(Parameter, 0x19);
    Write(Parameter, 0x31);
    Write(Parameter, 0x37);

    Write(Command, 0xE4);
    Write(Parameter, 0x1D); // 使用240根gate  (N+1)*8
    Write(Parameter, 0x00); // 设定gate起点位置
    Write(Parameter, 0x00); // 当gate没有用完时，bit4(TMG)设为0

    Write(Command, 0x21);

    Write(Command, 0x29);

    Write(Command, 0x2A); // Column Address Set
    Write(Parameter, 0x00);
    Write(Parameter, 0x00); // 0
    Write(Parameter, 0x00);
    Write(Parameter, 0xEF); // 239

    Write(Command, 0x2B); // Row Address Set
    Write(Parameter, 0x00);
    Write(Parameter, 0x00); // 0
    Write(Parameter, 0x00);
    Write(Parameter, 0xEF); // 239

    Write(Command, 0x2C);
}

void C096LAB_Init(void)
{
    LCD_RSTHigh();
    HAL_Delay(120);
    LCD_RSTLow();
    HAL_Delay(120);
    LCD_RSTHigh();
    HAL_Delay(200);

    HAL_Delay(120); // HAL_Delay 120ms

    wr_cmd_4spi_8bcs(0x11); // Sleep out

    HAL_Delay(120); // HAL_Delay 120ms

    wr_cmd_4spi_8bcs(0xB1);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x3A);

    wr_cmd_4spi_8bcs(0xB2);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x3A);

    wr_cmd_4spi_8bcs(0xB3);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x3A);
    wr_dat_4spi_8bcs(0x3A);

    wr_cmd_4spi_8bcs(0xB4); // Dot inversion
    wr_dat_4spi_8bcs(0x03);

    wr_cmd_4spi_8bcs(0xC0);
    wr_dat_4spi_8bcs(0x44);
    wr_dat_4spi_8bcs(0x04);
    wr_dat_4spi_8bcs(0x04);

    wr_cmd_4spi_8bcs(0xC1);
    wr_dat_4spi_8bcs(0xC0);

    wr_cmd_4spi_8bcs(0xC2);
    wr_dat_4spi_8bcs(0x0D);
    wr_dat_4spi_8bcs(0x00);

    wr_cmd_4spi_8bcs(0xC3);
    wr_dat_4spi_8bcs(0x8D);
    wr_dat_4spi_8bcs(0x6A);

    wr_cmd_4spi_8bcs(0xC4);
    wr_dat_4spi_8bcs(0x8D);
    wr_dat_4spi_8bcs(0xEE);

    wr_cmd_4spi_8bcs(0xC5);
    wr_dat_4spi_8bcs(0x14);

    wr_cmd_4spi_8bcs(0xE0);
    wr_dat_4spi_8bcs(0x06);
    wr_dat_4spi_8bcs(0x1C);
    wr_dat_4spi_8bcs(0x16);
    wr_dat_4spi_8bcs(0x16);
    wr_dat_4spi_8bcs(0x36);
    wr_dat_4spi_8bcs(0x2F);
    wr_dat_4spi_8bcs(0x28);
    wr_dat_4spi_8bcs(0x2A);
    wr_dat_4spi_8bcs(0x27);
    wr_dat_4spi_8bcs(0x24);
    wr_dat_4spi_8bcs(0x2C);
    wr_dat_4spi_8bcs(0x35);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x03);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x10);

    wr_cmd_4spi_8bcs(0xE1);
    wr_dat_4spi_8bcs(0x05);
    wr_dat_4spi_8bcs(0x1A);
    wr_dat_4spi_8bcs(0x0F);
    wr_dat_4spi_8bcs(0x0E);
    wr_dat_4spi_8bcs(0x2E);
    wr_dat_4spi_8bcs(0x2A);
    wr_dat_4spi_8bcs(0x24);
    wr_dat_4spi_8bcs(0x29);
    wr_dat_4spi_8bcs(0x29);
    wr_dat_4spi_8bcs(0x27);
    wr_dat_4spi_8bcs(0x2F);
    wr_dat_4spi_8bcs(0x3C);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x02);
    wr_dat_4spi_8bcs(0x02);
    wr_dat_4spi_8bcs(0x10);

    wr_cmd_4spi_8bcs(0x35);
    wr_dat_4spi_8bcs(0x00);

    wr_cmd_4spi_8bcs(0x3A); // 65k mode
    wr_dat_4spi_8bcs(0x66);

    wr_cmd_4spi_8bcs(0x36);
    wr_dat_4spi_8bcs(0xC0);

    wr_cmd_4spi_8bcs(0x29); // Display on

    wr_cmd_4spi_8bcs(0x2C);

    wr_cmd_4spi_8bcs(0x2A);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x00 + 2);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x7F + 2);
    wr_cmd_4spi_8bcs(0x2B);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x00 + 1);
    wr_dat_4spi_8bcs(0x00);
    wr_dat_4spi_8bcs(0x9F + 1);
}
